package com.example.smartcity.logic.utils

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity.SmartCityApplication.Companion.context
import kotlin.math.abs

class MIXRecyclerView(context: Context) : RecyclerView(context){
    private var startX: Int? = null
    private var startY: Int? = null

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        when(ev?.action){
            MotionEvent.ACTION_DOWN -> {
                startX = ev.x.toInt()
                startY = ev.y.toInt()
                parent.requestDisallowInterceptTouchEvent(true)
            }

            MotionEvent.ACTION_MOVE -> {
                val endX = ev.x
                val endY = ev.y
                var disX = abs(endX - startX!!)
                var disY = abs(endY - startY!!)
                if (disX > disY){
                    //如果是水平滑动，并且没有滑动到顶部或底部，则通知父布局不进行事件拦截，交由子布局消费
                    parent.requestDisallowInterceptTouchEvent(canScrollHorizontally(startX!! - endX.toInt()))
                }else {
                    //如果是垂直滑动，并且没有滑动到顶部或底部，则通知父布局不进行事件拦截，交由子布局消费
                    parent.requestDisallowInterceptTouchEvent(canScrollVertically(startY!! - endY.toInt()))
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }
}